package com.foreverwin.jzymes.production.program.controller;

import com.foreverwin.jzymes.common.service.IResourceAdviceService;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.annotation.ParamBind;
import com.foreverwin.jzymes.framework.base.AjaxResult;
import com.foreverwin.jzymes.framework.base.BaseController;
import com.foreverwin.jzymes.framework.exception.BusinessException;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.framework.util.ObjectMapperUtil;
import com.foreverwin.jzymes.production.program.domain.CncProgram;
import com.foreverwin.jzymes.production.program.domain.CncProgramItem;
import com.foreverwin.jzymes.production.program.service.ICncProgramAdviceService;
import com.foreverwin.jzymes.production.program.service.ICncProgramItemService;
import com.foreverwin.jzymes.production.program.service.ICncProgramService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cncProgram")
public class CncProgramController extends BaseController {

    @Autowired
    ICncProgramAdviceService cncProgramAdviceService;
    @Autowired
    ICncProgramService cncProgramService;
    @Autowired
    ICncProgramItemService cncProgramItemService;
    @Autowired
    IResourceAdviceService resourceAdviceService;

    @RequestMapping("/saveCncProgram")
    @ParamBind
    public AjaxResult saveCncProgram( CncProgram cncProgram, ArrayList<CncProgramItem> cncProgramItems ){

        try{
            cncProgram.setSite( CommonMethods.getSite() );
            cncProgramAdviceService.saveCncProgram( cncProgram, cncProgramItems );
            return AjaxResult.returnSuccess();
        }catch( BusinessException e ){
            return AjaxResult.returnError( e.getMessage() );
        }
    }

    @RequestMapping(value = "/uploadProgram")
    public AjaxResult uploadProgram( @RequestParam MultipartFile file, @RequestParam String machineType, @RequestParam String cncType, @RequestParam String prgmCode, @RequestParam String revision, @RequestParam String subSeq ){

        try{
            List<Map<String,String>> fileList = cncProgramAdviceService.uploadProgram( file, machineType, cncType, prgmCode, revision, subSeq );
            return AjaxResult.returnSuccess().put( "value", fileList );
        }catch( BusinessException e ){
            return AjaxResult.returnError( e.getMessage() );
        }
    }

    @RequestMapping(value = "/downloadProgram")
    public void downloadProgram( String paramJson, HttpServletResponse response, HttpServletRequest request ){
        try {
            Map<String,Object> paramMap = ObjectMapperUtil.readValue( paramJson, HashMap.class );
            CncProgram cncProgram = ObjectMapperUtil.convertValue( paramMap.get( "cncProgram" ), CncProgram.class );
            List<CncProgramItem> prgmList = ObjectMapperUtil.convertList( paramMap.get( "prgmList" ), CncProgramItem.class );
            File zipFile = cncProgramAdviceService.zipProgramItem( cncProgram, prgmList );
            fileDownload( zipFile, true, response, request );
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/uploadProgramToCnc")
    public AjaxResult uploadProgramToCnc( @RequestParam String resrce, @RequestParam String machineType, @RequestParam String cncType, @RequestParam String prgmCode, @RequestParam String revision, @RequestParam String subSeq, @RequestParam String programFile ){
        try{
            String site = CommonMethods.getSite();
            cncProgramAdviceService.uploadProgramToCnc( site, resrce, machineType, cncType, prgmCode, revision, subSeq, programFile );
            return AjaxResult.returnSuccess();
        }catch( BusinessException e ){
            return AjaxResult.returnError( e.getMessage() );
        }

    }

    @RequestMapping(value = "/downloadProgramFromCnc")
    public void downloadProgramFromCnc( @RequestParam String resrce, @RequestParam String machineType, @RequestParam String cncType, HttpServletResponse response, HttpServletRequest request ){
        try {
            String site = CommonMethods.getSite();
            File zipFile = cncProgramAdviceService.downloadProgramFromCnc( site, resrce );
            fileDownload( zipFile, true, response, request );
        } catch (BusinessException e) {
            response.setCharacterEncoding("utf-8");
            response.setContentType( "text/html;charset=UTF-8" );
            try {
                response.getWriter().println( e.getMessage() );
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    @RequestMapping(value = "/findCncProgramList")
    public AjaxResult findCncProgramList( @RequestParam String machineType, @RequestParam String cncType ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));

        CncProgram queryParam = new CncProgram();
        queryParam.setSite( site );
        queryParam.setMachineType( machineType );
        queryParam.setCncType( cncType );

        List<CncProgram> cncPrograms = cncProgramService.selectCncProgramList( queryParam );
        return AjaxResult.returnSuccess().put( "value", cncPrograms  );
    }

    @RequestMapping("/findCncProgram")
    public AjaxResult findCncProgram( @RequestParam String machineType, @RequestParam String cncType, @RequestParam String prgmCode, @RequestParam String revision, @RequestParam String subSeq ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));

        try{
            CncProgram cncProgram = cncProgramAdviceService.findCncProgram( site, machineType, cncType, prgmCode, revision, subSeq );

            if( cncProgram != null ){
                CncProgramItem queryParam = new CncProgramItem();
                queryParam.setCncPrgmBo( cncProgram.getHandle() );
                List<CncProgramItem> cncProgramItems = cncProgramItemService.selectCncProgramItemList( queryParam );

                Map<String,Object> returnMap = new HashMap<>();
                Map<String,Object> cncProgramMap = new HashMap<>();
                cncProgramMap.put( "machineType", cncProgram.getMachineType() );
                cncProgramMap.put( "prgmCode", cncProgram.getPrgmCode() );
                cncProgramMap.put( "revision", cncProgram.getRevision() );
                cncProgramMap.put( "subSeq", cncProgram.getSubSeq() );
                cncProgramMap.put( "currentRevision", cncProgram.getCurrentRevision() );
                cncProgramMap.put( "prgmFile", cncProgram.getPrgmFile() );
                cncProgramMap.put( "toolBillBo", cncProgram.getToolBillBo() );
                cncProgramMap.put( "toolBillRevision", BOHandleUtil.splitToolBillHandle( cncProgram.getToolBillBo() )[2] );
                returnMap.put( "cncProgram", cncProgramMap );
                returnMap.put( "progList", cncProgramItems );

                return AjaxResult.returnSuccess().put("value", returnMap );
            }

            return AjaxResult.returnSuccess();
        }catch ( BusinessException e ){
            return AjaxResult.returnError(e.getMessage());
        }
    }

    @RequestMapping("/findAutoResource")
    public AjaxResult findAutoResource(){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        List<Map<String,Object>> resourceList = resourceAdviceService.findAutoLineResource( site );
        List<Map<String,String>> rtnResourceList = new ArrayList<>();
        for( Map<String,Object> resourceMap : resourceList ){
            Map<String,String> rtnResourceMap = new HashMap<>();
            rtnResourceMap.put( "resrce", (String) resourceMap.get( "RESRCE" ));
            rtnResourceMap.put( "description", (String) resourceMap.get( "DESCRIPTION" ));
            rtnResourceList.add( rtnResourceMap );
        }

        return AjaxResult.returnSuccess().put( "value", rtnResourceList );
    }

    @RequestMapping("/existAutoResource")
    public AjaxResult existAutoResource( @RequestParam String resrce ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));

        Map<String,Object> resourceMap = resourceAdviceService.existResource( site, resrce );

        List<Map<String,Object>> resourceList = resourceAdviceService.findAutoLineResource( site );
        Map<String,Object> rtnResourceMap = new HashMap<>();
        if( resourceList.contains( resourceMap ) ){
            rtnResourceMap.put( "resrce", resourceMap.get( "RESRCE" ) );
            rtnResourceMap.put( "description", resourceMap.get( "DESCRIPTION" ) );
        }
        if( rtnResourceMap.isEmpty() ){
            throw BusinessException.build( "common.resourceNotValid", new String[]{ resrce } );
        }else{
            return AjaxResult.returnSuccess().put( "value", rtnResourceMap );
        }
    }

}
